

#  833. 字符串中的查找与替换

def findReplaceString(s, indices, sources, targets):
    for i in range(0,len(indices)):
        for j in range(i,len(indices)):
            if indices[i]>indices[j]:
                temp_indices = indices[i]
                indices[i] = indices[j]
                indices[j] = temp_indices
                temp_sources = sources[i]
                sources[i] = sources[j]
                sources[j] = temp_sources
                temp_targets = targets[i]
                targets[i] = targets[j]
                targets[j] = temp_targets
    result = s
    nums = 0
    jian = 0
    for i in range(0, len(indices)):
        if s[indices[i]:indices[i] + len(sources[i])] == sources[i]:
            front = result[0:indices[i]+nums-jian]
            behind = result[indices[i] + len(sources[i]) + nums-jian:]
            result = front + targets[i] + behind
            nums = nums + len(targets[i])
            jian = jian + len(sources[i])
    return result


if __name__ == "__main__":
    s = "vmokgggqzp"
    indices = [3,5,1]
    sources = ["kg","ggq","mo"]
    targets = ["s","so","bfr"]
    print(findReplaceString(s,indices,sources,targets))